package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.AdministratorsDao;
import com.guoyansoft.model.pojo.Administrators;
import com.guoyansoft.model.service.AdministratorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 刘耀诚
 * 管理员service实现类
 */
@Service
public class AdministratorsServiceImpl implements AdministratorsService {
    @Autowired
    private AdministratorsDao administratorsDao;
    @Override
    public R save(Administrators administrators) {
        String newPass = PasswordEncryption.getCiphertext(administrators.getPass());
        administrators.setPass(newPass);
        int row = administratorsDao.insert(administrators);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer id) {
        int row = administratorsDao.delete(id);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R modify(Administrators administrators) {
        String newPass = PasswordEncryption.getCiphertext(administrators.getPass());
        administrators.setPass(newPass);
        int row = administratorsDao.update(administrators);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R queryByPage(Map<String,Object> params) {
        List<Administrators> administrators = administratorsDao.selectAll(params);
        return R.suc(administrators);
    }

    @Override
    public R queryOfLogin(Administrators administrators) {
        String newPass = PasswordEncryption.getCiphertext(administrators.getPass());
        administrators.setPass(newPass);
        Administrators administrators1 = administratorsDao.selectOfLogin(administrators);
        return administrators1==null?R.fail():R.suc(administrators1);
    }
}
